package com.example.fw;

import com.example.tests.AddressData;
import com.example.utils.SortedListOf;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: pshevchenko
 * Date: 06.03.13
 * Time: 18:58
 * To change this template use File | Settings | File Templates.
 */
public class AddressHelper extends WebDriverHelperBase {

    private List<String> cachedGroupNames;

    private AddressData readAddressDataFromList(int addressIndex) {
        AddressData resultAddress = new AddressData();
        resultAddress.setFirstName(getFieldText(By.xpath(".//*[@id='maintable']/tbody/tr[" + (addressIndex + 2) + "]/td[3]")));

        resultAddress.setLastName(getFieldText(By.xpath(".//*[@id='maintable']/tbody/tr[" + (addressIndex + 2) + "]/td[2]")));

        String eMailString = getFieldAttribute(By.xpath(".//*[@id='maintable']/tbody/tr[" + (addressIndex + 2) + "]/td[4]/a"), "href");
        resultAddress.seteMail1(eMailString.substring("mailto:".length(), eMailString.length()));

        resultAddress.setWorkPhone(getFieldText(By.xpath(".//*[@id='maintable']/tbody/tr[" + (addressIndex + 2) + "]/td[5]")));

        return resultAddress;
    }

//----------------------------------------------------------------------------------------------------------------------

    public void createAddress(AddressData addressData) {
        manager.navigateTo().mainPage();
        gotoAddressCreation();
        addressData.selectRandomGroup(getGroupNameDropdownList());
        fillAddressForm(addressData, CREATION);
        submitAddressCreation();
        returnToHomePage();
        manager.getModel()
                .addAddress(addressData);
    }

    public AddressData modifyAddress(AddressData addressData, int index) {
        initAddressModification(index);
        AddressData expectedAddressData = fillAddressModificationFormAndSubmit(addressData, index);
        return expectedAddressData;
    }

    public void initAddressModification(int index) {
        manager.navigateTo().mainPage();
        gotoAddressModificationByIndex(index);
    }

    public AddressData fillAddressModificationFormAndSubmit(AddressData addressData, int index) {
        fillAddressForm(addressData, MODIFICATION);
        AddressData expectedAddressData = addressData.storeUnChangedFields(readAddressDataFromCard());
        submitAddressModification();
        returnToHomePage();
        manager.getModel()
                .removeAddress(index)
                .addAddress(expectedAddressData);
        return expectedAddressData;
    }

    public void deleteAddress(int index) {
        initAddressModification(index);
        initiateAddressRemoval();
        returnToHomePage();
        manager.getModel()
                .removeAddress(index);
    }
//----------------------------------------------------------------------------------------------------------------------

    public static final boolean CREATION = true;
    public static final boolean MODIFICATION = false;

    public AddressHelper(ApplicationManager manager) {
        super(manager);
    }

    public SortedListOf<AddressData> getGuiAddressList(){
        SortedListOf<AddressData> guiAddressList = new SortedListOf<AddressData>();
        List<WebElement> checkBoxList = driver.findElements(By.xpath(".//*[@type=\"checkbox\"][@name=\"selected[]\"]"));
        int addressCount = checkBoxList.size();
        for (int i = 0; i < addressCount; i++){
            AddressData nextAddressData = readAddressDataFromList(i);
            guiAddressList.add(nextAddressData);
        }
        return guiAddressList;
    }

    public AddressData readAddressDataFromCard()
    {
        AddressData result = new AddressData();
        result.setFirstName(readFirstNameFromCard());
        result.setLastName(readLastNameFromCard());
        result.setAddressString          (readAddressStringFromCard());
        result.setHomePhone              (readHomePhoneFromCard());
        result.setCellPhone              (readCellPhoneFromCard());
        result.setWorkPhone              (readWorkPhoneFromCard());
        result.seteMail1                 (readeMail1FromCard());
        result.seteMail2                 (readeMail2FromCard());
        result.setBirthDay               (readBirthDayFromCard());
        result.setBirthMonth             (readBirthMonthFromCard());
        result.setBirthYear              (readBirthYearFromCard());
        result.setAddressString2         (readAddressString2FromCard());
        result.setHomePhone2             (readHomePhone2FromCard());
        return result;
    }

    public String readFirstNameFromCard(){
        return getFieldValue(By.name("firstname"));
    }

    public String readLastNameFromCard(){
        return getFieldValue(By.name("lastname"));
    }

    public String readAddressStringFromCard(){
        return getFieldValue(By.name("address"));
    }

    public String readHomePhoneFromCard(){
        return getFieldValue(By.name("home"));
    }

    public String readCellPhoneFromCard(){
        return getFieldValue(By.name("mobile"));
    }
    public String readWorkPhoneFromCard(){
        return getFieldValue(By.name("work"));
    }
    public String readeMail1FromCard(){
        return getFieldValue(By.name("email"));
    }
    public String readeMail2FromCard(){
        return getFieldValue(By.name("email2"));
    }
    public String readBirthDayFromCard(){
        return getFieldValue(By.name("bday"));
    }
    public String readBirthMonthFromCard(){
        return getFieldValue(By.name("bmonth"));
    }
    public String readBirthYearFromCard(){
        return getFieldValue(By.name("byear"));
    }
    public String readAddressString2FromCard(){
        return getFieldValue(By.name("address2"));
    }
    public String readHomePhone2FromCard(){
        return getFieldValue(By.name("phone2"));
    }

    public AddressHelper returnToHomePage() {
        clickElement(By.linkText("home page"));
        return this;
    }

    public AddressHelper fillAddressForm(AddressData addressData, boolean formType) {
        fillTextField(By.name("firstname"), addressData.getFirstName());
        fillTextField(By.name("lastname"), addressData.getLastName());
        fillTextField(By.name("address"), addressData.getAddressString());
        fillTextField(By.name("home"), addressData.getHomePhone());
        fillTextField(By.name("mobile"), addressData.getCellPhone());
        fillTextField(By.name("work"), addressData.getWorkPhone());
        fillTextField(By.name("email"), addressData.geteMail1());
        fillTextField(By.name("email2"), addressData.geteMail2());
        selectComboByText(By.name("bday"), addressData.getBirthDay());
        selectComboByText(By.name("bmonth"), addressData.getBirthMonth());
        fillTextField(By.name("byear"), addressData.getBirthYear());
        fillTextField(By.name("address2"), addressData.getAddressString2());
        fillTextField(By.name("phone2"), addressData.getHomePhone2());
        if (formType == CREATION)
            selectComboByText(By.name("new_group"), addressData.getGroupName());
        return this;
    }

    public AddressHelper submitAddressCreation() {
        clickElement(By.name("submit"));
        return this;
    }

    public AddressHelper gotoAddressCreation() {
        clickElement(By.linkText("add new"));
        return this;
    }

    public AddressHelper gotoAddressModificationByIndex(int addressIndex) {
        clickElement(By.xpath("//tr[" + (addressIndex+2) + "]/td/a[starts-with(@href,\"edit.php\")]"));
        return this;
    }

    public AddressHelper gotoAddressModificationFromCard() {
        clickElement(By.xpath("//form/input[@value=\"Modify\"]"));
        return this;
    }

    public AddressHelper submitAddressModification() {
        clickElement(By.xpath("//form/input[@value=\"Update\"]"));
        return this;
    }

    public AddressHelper initiateAddressRemoval() {
        clickElement(By.xpath("//form/input[@value=\"Delete\"]"));
        return this;
    }

    public AddressHelper gotoAddressCardByIndex(int addressIndex) {
        clickElement(By.xpath("//tr[" + (addressIndex+2) + "]/td/a[starts-with(@href,\"view.php\")]"));
        return this;
    }

    /*public SortedListOf<AddressData> getSortedAddressList(){
        if (cachedAddressList == null)
            rebuildCache();
        return cachedAddressList;
    }*/

    public AddressHelper reportGroupModification()
    {
        cachedGroupNames = null;
        return this;
    }

    public List<String> getGroupNameDropdownList(){
        if (cachedGroupNames == null)
            cachedGroupNames = getDropDownList("new_group");
        return cachedGroupNames;
    }

}
